/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Configuration;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{

  [Image(typeof(SqlQueryNode))]
  [SelectedImage(typeof(SqlQueryNode))]
  public class SqlQueryNode : ConfigurationNode
  {
    private string sql;
    private QueryBuilderNode queryBuilderNode;
    private EventHandler<ConfigurationNodeChangedEventArgs> queryBuilderNodeRemovedHandler;

    public SqlQueryNode()
	    : this(new SqlQueryData(Resource.DefaultSqlQueryName))
    {
        
    }

    public SqlQueryNode(SqlQueryData data)
      : base((data == null) ? Resource.DefaultSqlQueryName : data.Name)
    {
      if (data == null) { throw new ArgumentNullException("SqlQueryData"); }
      this.queryBuilderNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnQueryBuilderNodeRemoved);
      if (data.Mappings != null)
      {
        foreach (MappingReferenceData refdata in data.Mappings)
        {
          this.AddNode(new ObjectMappingReferenceNode(refdata));
        }
      }
      this.sql = data.Sql;
    }		

    [Browsable(false)]
    public SqlQueryData SqlQueryData
    {
      get
      {
        SqlQueryData data = new SqlQueryData(Name);
        data.Sql = this.sql;
        if (this.QueryBuilder != null)
        {
          data.QueryBuilder = this.QueryBuilder.Name;
        }
        if (this.Nodes != null)
        {
          foreach (ObjectMappingReferenceNode map in this.Nodes)
          {
            data.Mappings.Add(map.MappingData);
          }
        }
	      return data;
      }
    }

    [Required]
    [SRDescription("SqlQuerySqlDescription", typeof(Resource))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public string Sql
    {
      get { return sql; }
      set { sql = value; }
    }


    [SRDescription("QueryBuilderReferenceDescription", typeof(Resource))]
    [Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
    [ReferenceType(typeof(QueryBuilderNode))]
    [SRCategory("CategoryGeneral", typeof(Resource))]
    public QueryBuilderNode QueryBuilder
    {
      get { return queryBuilderNode; }
      set
      {
        queryBuilderNode = LinkNodeHelper.CreateReference<QueryBuilderNode>(queryBuilderNode,
                                                                    value,
                                                                    queryBuilderNodeRemovedHandler,
                                                                    null);
      }
    }

    protected override void Dispose(bool disposing)
    {
      try
      {
        if (disposing)
        {
          if (null != queryBuilderNode)
          {
            queryBuilderNode.Removed -= queryBuilderNodeRemovedHandler;
          }
        }
      }
      finally
      {
        base.Dispose(disposing);
      }
    }
    protected override void OnChildAdded(ConfigurationNodeChangedEventArgs e)
    {
      base.OnChildAdded(e);
      ObjectMappingReferenceNode node = e.Node as ObjectMappingReferenceNode;
      Debug.Assert(null != node, "Only ObjectMappingReferenceNode objects can be added to a ObjectSqlMappingNode");

    }
    private void OnQueryBuilderNodeRemoved(object sender, ConfigurationNodeChangedEventArgs e)
    {
      this.queryBuilderNode = null;
    }
	}
}